package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.windoent.yugioh.R;
import com.windoent.yugioh.bean.OnGoingDuiZhenBean;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.SharedPreUtil;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.TDevice;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.RoundImageView;

/**
 * 作者：Android
 * 时间：2022/1/16 11:08
 * 功能：繼承Dialog，自定义更新Dialog
 */
public class SubmitGradeAgainstDialog extends Dialog {
    private RoundImageView mIvAvatar;
    private TextView mTvName;
    private TextView mTvId;
    private TextView mTvUnselectVictory;
    private TextView mTvSelectedVictory;
    private ImageView mIvVictory;
    private TextView mTvUnselectPing;
    private TextView mTvSelectedPing;
    private TextView mTvUnselectFail;
    private TextView mTvSelectedFail;
    private ImageView mIvFail;
    private TextView mTvNo;
    private TextView mTvYes;
    private RelativeLayout mLlVictorySelected;
    private RelativeLayout mLlPingSelected;
    private RelativeLayout mLlFailSelected;
    private int selectPosition = 0;//1胜利，2平，3失败

    private onClickListener listener;
    private OnGoingDuiZhenBean.DataBean.ResBean item;
    private boolean outGame = false;

    public void setOnclickListener(onClickListener clickListener) {
        this.listener = clickListener;
    }

    public SubmitGradeAgainstDialog(Context context) {
        super(context, R.style.MoHUDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_sumit_grade_against_dialog_layout);
        //按空白处能取消动画
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.BOTTOM);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        mLlVictorySelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSelectText(true, false, false);
                selectPosition = 1;
            }
        });
        mLlPingSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSelectText(false, true, false);
                selectPosition = 2;
            }
        });
        mLlFailSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSelectText(false, false, true);
                selectPosition = 3;
            }
        });
        mTvYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectPosition == 0) {
                    ToastUtils.showText(getContext().getString(R.string.string_select_one_to_submit));
                    return;
                }
                if (listener != null) {
                    listener.onClick(selectPosition);
                }
                dismiss();
            }
        });
        mTvNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        showSelectText(false, false, false);
        if (item != null) {
            setUI(item);
        }
        showPingUi(outGame);
    }

    //用户id
    private int getAccountId() {
        return SharedPreUtil.getInt(getContext(), Constants.USERID, 0);
    }

    //显示自己信息
    private void setUI(OnGoingDuiZhenBean.DataBean.ResBean item) {
        if (item.getA_id() == getAccountId()) {
            SimpleUtils.loadImageForView(getContext(), mIvAvatar, item.getA_avatar(), R.drawable.img_official_avatar_android);
            mTvName.setText(BaseMethod.notNullDefaultValueNull(item.getA_nickname()));
            mTvId.setText(getContext().getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(item.getA_id() + ""));
        } else if (item.getB_id() != 0 && item.getB_id() == getAccountId()) {
            SimpleUtils.loadImageForView(getContext(), mIvAvatar, item.getB_avatar(), R.drawable.img_official_avatar_android);
            mTvName.setText(BaseMethod.notNullDefaultValueNull(item.getB_nickname()));
            mTvId.setText(getContext().getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(item.getB_id() + ""));
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {

        mIvAvatar = (RoundImageView) findViewById(R.id.iv_avatar);
        mTvName = (TextView) findViewById(R.id.tv_name);
        mTvId = (TextView) findViewById(R.id.tv_id);
        mTvUnselectVictory = (TextView) findViewById(R.id.tv_unselect_victory);
        mTvSelectedVictory = (TextView) findViewById(R.id.tv_selected_victory);
        mIvVictory = (ImageView) findViewById(R.id.iv_victory);
        mTvUnselectPing = (TextView) findViewById(R.id.tv_unselect_ping);
        mTvSelectedPing = (TextView) findViewById(R.id.tv_selected_ping);
        mTvUnselectFail = (TextView) findViewById(R.id.tv_unselect_fail);
        mTvSelectedFail = (TextView) findViewById(R.id.tv_selected_fail);
        mIvFail = (ImageView) findViewById(R.id.iv_fail);
        mTvNo = (TextView) findViewById(R.id.tv_no);
        mTvYes = (TextView) findViewById(R.id.tv_yes);
        mLlVictorySelected = (RelativeLayout) findViewById(R.id.ll_victory_selected);
        mLlPingSelected = (RelativeLayout) findViewById(R.id.ll_ping_selected);
        mLlFailSelected = (RelativeLayout) findViewById(R.id.ll_fail_selected);

    }

    private void showPingUi(boolean outGame) {
        mLlPingSelected.setVisibility(outGame ? View.GONE : View.VISIBLE);
    }

    public interface onClickListener {
        public void onClick(int selectPosition);
    }

    //选中显示
    private void showSelectText(boolean victory, boolean ping, boolean fail) {
        mTvUnselectVictory.setVisibility(victory ? View.INVISIBLE : View.VISIBLE);
        mTvSelectedVictory.setVisibility(!victory ? View.INVISIBLE : View.VISIBLE);
        mIvVictory.setVisibility(!victory ? View.INVISIBLE : View.VISIBLE);
        mTvUnselectPing.setVisibility(ping ? View.INVISIBLE : View.VISIBLE);
        mTvSelectedPing.setVisibility(!ping ? View.INVISIBLE : View.VISIBLE);
        mTvUnselectFail.setVisibility(fail ? View.INVISIBLE : View.VISIBLE);
        mTvSelectedFail.setVisibility(!fail ? View.INVISIBLE : View.VISIBLE);
        mIvFail.setVisibility(!fail ? View.INVISIBLE : View.VISIBLE);
    }

    public void setDataInfo(OnGoingDuiZhenBean.DataBean.ResBean item, boolean outGame) {
        this.outGame = outGame;
        this.item = item;
    }
}
